// 表尾合计
var utils = {
  back(){
    history.back();
  },
  getERPSummaries(param) {
    const {
      columns,
      data
    } = param;
    const sums = [];
    columns.forEach((column, index) => {
      if (index === 0) {
        sums[index] = '总价';
        return;
      }
      if (index == 1 || index == 2 || index >= 4) {
        sums[index] = '--';
        return;
      }
      const values = data.map(item => Number(item[column.property]));
      if (!values.every(value => isNaN(value))) {
        sums[index] = values.reduce((prev, curr) => {
          const value = Number(curr);
          if (!isNaN(value)) {
            return prev + curr;
          } else {
            return prev;
          }
        }, 0);
        if (index == 4) {
          sums[index] += '';
          return;
        }
        sums[index] = Number(sums[index]);
        var b = sums[index].toFixed(3);
        sums[index] = b.substring(0, b.toString().length - 1);
        sums[index] += ' 元';
      } else {
        sums[index] = 'N/A';
      }
    });
    return sums;
  },
  getERPSummariesGoods(param) {
    const {
      columns,
      data
    } = param;
    const sums = [];
    columns.forEach((column, index) => {
      if (index === 0) {
        sums[index] = '总价';
        return;
      }
      if (index == 1 || index == 3 || index == 5) {
        sums[index] = '--';
        return;
      }
      const values = data.map(item => Number(item[column.property]));
      if (!values.every(value => isNaN(value))) {
        sums[index] = values.reduce((prev, curr) => {
          const value = Number(curr);
          if (!isNaN(value)) {
            return prev + curr;
          } else {
            return prev;
          }
        }, 0);
        if (index == 2) {
          sums[index] += '';
          return;
        }
        sums[index] = Number(sums[index]);
        var b = sums[index].toFixed(3);
        sums[index] = b.substring(0, b.toString().length - 1);
        sums[index] += ' 元';
      } else {
        sums[index] = 'N/A';
      }
    });
    return sums;
  }
}

export default utils;